class Login {
    constructor() {
        //登陆键的点击事件
        this.$('.loginSmallDiv .redButton').addEventListener('click', this.login)
    }
    //登陆方法
    login = () => {
        // console.log(11111);
        //获取账号和密码
        let name = this.$('.loginSmallDiv #name').value;
        let password = this.$('.loginSmallDiv #password').value;
        // console.log(name,password);
        //设置发送neirong
        let str = `username=${name}&password=${password}`;
        // 设置请求头
        axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        //发送至后台进行确认
        axios.post('http://localhost:8888/users/login',str)
        .then((res)=>{
            // console.log(res);
            let {data,status}=res;
            console.log(data,status);
            if(data.code==1&&status==200){
                localStorage.setItem('id',data.user.id);
                localStorage.setItem('token',data.token);
                //设置返回来处
                this.comeBack();
            }else{
                alert('登录失败, 用户名或者密码不对')
            }
        })
    }
    comeBack(){
        // 获取网址中的信息
        let mess = location.search;
        // console.log(mess);
        if(mess){
            let comeFrom=mess.split('=').pop();
            // console.log(comeFrom);
            location.assign(comeFrom);
        }else{
            location.assign('index.html');
        }
    }
    /* *****获取节点的方法****** */
    $(ele) {
        let node = document.querySelectorAll(ele)
        return node.length == 1 ? node[0] : node;
    }
}
new Login;